package com.beiding.my.bomapping;

import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpressionDetails {

    private String name;

    ExpressionDetails(String name) {
        this.name = name;
        lastQuoteDetails = new QuoteDetails();
        lastQuoteDetails.statement = name;

    }

    //涉及到的id区段(可用于 as 声明)
    List<String> quoteIds = new ArrayList<>();

    //表名的声明结构(可用于生成关联性条件)
    List<QuoteDetails> quoteDetails = new ArrayList<>();

    //最后一个引用详情可结合lastField生成真实返回
    QuoteDetails lastQuoteDetails;

    public List<QuoteDetails> getQuoteDetails() {
        return quoteDetails;
    }

    private String pre;

    private Map<String, String> fieldToIdMap = new HashMap<>();


    private String getPre() {
        if (pre == null) {
            if (quoteIds.size() == 0) {
                pre = "";
            } else {
                pre = Joiner.on("_").join(quoteIds) + "_";
            }
        }
        return pre;
    }

    void set(String f, String id) {
        String s = lastQuoteDetails.statement + "." + f;
        fieldToIdMap.put(s, getPre() + id);
    }

    public Map<String, String> getFieldToIdMap() {
        return fieldToIdMap;
    }
}
